// AllFix.cpp: implementation of the AllFix class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "AllFix.h"
#include "Joint.h"
#include "Part.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
using namespace JointSystem;

AllFix::AllFix()
{

}

AllFix::~AllFix()
{

}

BOOL AllFix::Execute()
{
	GeneralJointRegular* pRegular = static_cast<GeneralJointRegular*>(GetJointRegular());

	ASSERT(pRegular);
	ASSERT(pRegular->GetJoint());
	ASSERT(pRegular->GetJoint()->GetPrev());
	ASSERT(pRegular->GetJoint()->GetNext());
	JointFunction* pPrevFunction = pRegular->GetJoint()->GetPrev();
	JointFunction* pNextFunction = pRegular->GetJoint()->GetNext();

/*	Part* pPart = pRegular->GetJoint()->GetPrev()->GetOwner();
	Part* pNextPart = pRegular->GetJoint()->GetNext()->GetOwner();
	ASSERT(pPart);
	ASSERT(pNextPart);*/

	JointPort* pFirstPort = pPrevFunction->jointPort();

	JointPort* pNextPort = pNextFunction->jointPort();

	if(!pNextPort->CanAttachTo(pFirstPort))
		return FALSE;
	
	pRegular->CaculateMatrix();
	return TRUE;
}

JointPort* AllFix::GetJointPort(JointPart& part, JointFunction* pFunction)
{
	ASSERT(pFunction->GetOwner());
	ASSERT(pFunction->GetOwner()->IsKindOf(UE_RUNTIME_CLASS(Part)));

	Part* pPart = static_cast<Part*>(pFunction->GetOwner());
	ASSERT(pPart);

	part.CatalogID(pPart->GetStandard());
	part.PartFormatID(pPart->GetCustomID());
	part.SetProperties(pPart->GetPropertyStack());
	return part.NewJointPort(*pFunction->GetFormer());
}